// let timeId=''

Page({
  data: {
    showChannel: false,
    showSearch: false,
    channelData: [],
    userChannel: [],
    channelOrder: [],
    recChannel: [],
    articleList: [],
    showDel: false,
    editText: "编辑",
    tabActive: 0,
    selected: "",
  },
  onLoad() {
    this.getUserChannel();
    this.getArticleList();
  },
  onShow() {
    getApp().setTabBar();
  },

  openSerach() {
    wx.navigateTo({
      url: "/pages/search/index",
    });
  },

  async getArticleList(id = 0) {
    const time = new Date().getTime();
    const res = await wx.http.get(
      `/v1_0/articles?channel_id=${id}&timestamp=${time}`
    );
    console.log("推荐文章列表", res);
    const temp = res.data.data.results;
    temp.forEach((item) => {
      item.pastTime = wx.utils.getPastTime(item.pubdate);
    });
    this.setData({
      articleList: this.data.articleList.concat(temp),
    });
  },

  async getChannelData() {
    const res = await wx.http.get("/v1_0/channels");
    if (!res.data) return;
    const channelData = res.data.data.channels;
    this.setData({
      channelData,
    });
    console.log("频道列表", channelData);
  },

  async getUserChannel() {
    const res = await wx.http.get("/v1_0/user/channels");
    const channelOrder = wx.getStorageSync("user-channel-order");
    console.log("本地order", channelOrder);
    let userChannel = res.data.data.channels;
    if (channelOrder[0] && getApp().token) {
      const temp = channelOrder.map((order) => {
        return userChannel.find((item) => item.id === order.id);
      });
      if (temp.every((item) => item !== undefined)) userChannel = temp;
    }
    this.setData({
      userChannel,
    });
    console.log("用户频道", this.data.userChannel);
  },

  getRecChannel() {
    const recChannel = this.data.channelData.filter((item) => {
      return !this.data.userChannel.some((item2) => item.id === item2.id);
    });
    this.setData({
      recChannel,
    });
    console.log("推荐频道", recChannel);
  },

  async openChannel() {
    await this.getChannelData();
    await this.getUserChannel();
    this.getRecChannel();
    this.setData({ showChannel: true });
  },

  async closeChannel() {
    this.setData({
      showChannel: false,
      showDel: false,
      editText: "编辑",
    });
  },
  toggleEdit() {
    this.setData({
      showDel: !this.data.showDel,
      editText: this.data.showDel ? "编辑" : "完成",
    });
  },

  async handleChannel(event) {
    const id = +event.target.id;
    if (this.data.showDel) {
      if (!getApp().token) return wx.utils.toast("对不起，您还未登录");
      if (+id === 0) return;
      const res = await wx.http.delete(`/v1_0/user/channels/${id}`);
      console.log("删除结果", res);
      if (!res) return wx.utils.toast("游客不能设置个性化频道");
      if (id === this.data.tabActive) {
        this.setData({
          tabActive: 0,
        });
      }
      const channelOrder = this.data.channelOrder.filter(
        (item) => item.id !== id
      );
      this.setOrder(channelOrder);
      await this.getChannelData();
      await this.getUserChannel();
      this.getRecChannel();
    } else {
      this.setData({
        tabActive: +id,
        articleList: [],
      });
      await this.getArticleList(id);
      this.closeChannel();
    }
  },

  async addChannel(event) {
    if (!getApp().token) return wx.utils.toast("对不起，您还未登录");
    console.log(event.target.id);
    const addItem = this.data.recChannel.find(
      (item) => item.id === +event.target.id
    );
    const newUserChannel = [...this.data.userChannel, addItem];
    console.log("新我的", newUserChannel);
    const channels = newUserChannel.map((item, index) => {
      return {
        id: item.id,
        name: item.name,
        seq: index,
      };
    });
    const res = await wx.http.put("/v1_0/user/channels", { channels });
    console.log("更新结果", res);
    if (!res) return;
    if (res.data.message !== "更新用户频道成功")
      return wx.utils.toast("更新失败");
    this.setOrder(res.data.data.channels);
    await this.getChannelData();
    await this.getUserChannel();
    this.getRecChannel();
  },

  setOrder(val) {
    this.setData({
      channelOrder: val,
    });
    wx.setStorageSync("user-channel-order", val);
  },

  clickTab(event) {
    console.log("点击tab", event.detail.name);
    this.setData({
      tabActive: event.detail.name,
    });
    console.log("acitve值", this.data.tabActive);
    this.setData({
      articleList: [],
    });
    this.getArticleList(event.detail.name);
  },

  onReachBottom() {
    this.getArticleList(this.data.tabActive);
  },

  onPullDownRefresh() {
    this.setData({
      articleList: [],
    });
    this.getArticleList(this.data.tabActive);
  },
});
